package com.gec.controller;

import com.gec.domain.PageBean;
import com.gec.domain.PageParam;
import com.gec.domain.Reim;
import com.gec.service.abst.ReimService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/*
 *  Type: Class
 *  Author: DawnFz.com
 *  Date: 2022/10/26 9:29
 *  Project: HotelSSM
 *  Version: 1.0
 *  Describe: [ReimController类]
 */
@Controller
@RequestMapping("/Reim")
public class ReimController extends BaseController
{

    private final ReimService reimService;

    public ReimController(ReimService reimService)
    {
        this.reimService = reimService;
    }

    //[/showLeaveForm]内部转发到请假页面
    @RequestMapping("/showReimForm")
    public String showReimForm()
    {
        return "reim/reimForm";
    }

    //[/submitLeave]提交请假
    @ResponseBody
    @RequestMapping(value = "/submitReim", produces = JSON)
    public String submitReim(Reim reim)
    {
        try
        {
            reimService.startProcess(reim);
            return sendSuccess();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

    @RequestMapping("/myInitiate")
    public String myInitiate()
    {
        return "reim/myInitiate";
    }

    @ResponseBody
    @RequestMapping(value = "/jsonInitiate", produces = JSON)
    public String jsonInitiate(@RequestParam("initiator") String initiator, PageParam param)
    {
        PageBean<Reim> pageBean;
        try
        {
            pageBean = reimService.queryInitiate(param, initiator);
            return pageBean.toJSON();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

    @RequestMapping("/waitMyApprove")
    public String waitMyApprove()
    {
        return "reim/waitMyApprove";
    }

    @ResponseBody
    @RequestMapping(value = "/jsonWaitMyApprove", produces = JSON)
    public String jsonWaitMyApprove(@RequestParam("assigneeId") String assigneeId, PageParam param)
    {
        PageBean<Reim> pageBean;
        try
        {
            pageBean = reimService.queryWaitMyApprove(param, assigneeId);
            return pageBean.toJSON();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

    @RequestMapping("/myApproved")
    public String myApproved()
    {
        return "reim/myApproved";
    }

    @ResponseBody
    @RequestMapping(value = "/jsonMyApproved", produces = JSON)
    public String jsonMyApproved(@RequestParam("assigneeId") String assigneeId, PageParam param)
    {
        PageBean<Reim> pageBean;
        try
        {
            pageBean = reimService.queryHistoryApproved(param, assigneeId);
            return pageBean.toJSON();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

    @RequestMapping("/taskDetail")
    public String taskDetail(@RequestParam("taskId") String taskId, Model model)
    {
        Reim reim;
        try
        {
            reim = reimService.queryMyTaskByTaskId(taskId);
            //将 Leave对象 放入模型域
            model.addAttribute(reim);
            return "reim/taskDetail";
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "redirect:/error/showError";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/doApprove", produces = JSON)
    //@RequestParam Map<String,Object> map 获取表单参数至map，注解无需参数
    public String doApprove(@RequestParam Map<String, Object> map)
    {
        try
        {
            reimService.completeMyApprove(map);
            return sendSuccess();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }


    @ResponseBody
    @RequestMapping(value = "/cancelApprove", produces = JSON)
    public String cancelApprove(String procInstId)
    {
        try
        {
            reimService.cancelMyTaskByProcessInstanceId(procInstId);
            return sendSuccess();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }
}
